package main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import tool.GetAdjMatrix;
import tool.GetVertex;
import tool.GetWeights;
import tool.Settings;

public class Graphic extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnShowEdge = new JButton("ShowEdge");
	private JButton btnHideEdge = new JButton("HideEdge");
	private static int[][] vertexMatrix= GetVertex.getVertex();
	private static int rows = vertexMatrix.length;
	private static int[][] edges = new int[rows][rows];
	private static double [][] weightsMatrix = GetWeights.getWeights();
	private static int[][] adjMatrix = GetAdjMatrix.getAdjMatrix();
	int magnification = 0;
	DrawGraphic dg = new DrawGraphic();
	
	Graphic(){
		JPanel panel = new JPanel();
		panel.add(btnShowEdge);
		panel.add(btnHideEdge);
		
		
		
		this.add(panel, BorderLayout.NORTH);
		this.add(dg, BorderLayout.CENTER);
		
		btnShowEdge.addActionListener(new ShowEdgeListener());
		btnHideEdge.addActionListener(new HideEdgeListener());
		
	}
	
	public static void main(String[] args){
		
		JFrame frame = new Graphic();
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screen = kit.getScreenSize();
		int screenWidth = screen.width;
		int screenHeight = screen.height;
		frame.setSize(4 * screenWidth/5, 4 * screenHeight/5);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle(Settings.frameTitle);
		frame.setVisible(true);
		
	}
	
	class ShowEdgeListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			dg.showEdge();
		}
	}
	
	class HideEdgeListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			dg.hideEdge();
		}
	}

	public class DrawGraphic extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		boolean isShowEdge = true;
		
		public void paintComponent(Graphics g) {

			super.paintComponent(g);
			g.translate(0, this.getHeight());
			g.setColor(Color.black);
			magnification = getMagnification(vertexMatrix);
			System.out.println(magnification);
			for (int i = 0; i < rows; i++) {
				int x = vertexMatrix[i][0];
				int y = vertexMatrix[i][1];
				g.fillOval(magnification * x, -magnification * y, 5, 5);
				g.drawString("(" + x + "," + y + ")", magnification * x - 15,
						-magnification * y + 18);
				g.drawString("P" + i, magnification * x - 5, -magnification * y
						- 3);
			}

			if (isShowEdge) {
				for (int i = 0; i < rows; i++) {
					for (int j = 0; j < rows; j++) {
						if (adjMatrix[i][j] == 1 && (i <= j)) {
							int point1X = vertexMatrix[i][0];
							int point1Y = vertexMatrix[i][1];
							int point2X = vertexMatrix[j][0];
							int point2Y = vertexMatrix[j][1];
							// double lineLongth = Math.sqrt(Math.pow(point2X -
							// point1X, 2) + Math.pow(point2Y - point1Y, 2));
							double lineLongth = weightsMatrix[i][j];
							int showLongthX = (int) (point2X + point1X) / 2;
							int showLongthY = (int) (point2Y + point1Y) / 2;
							g.drawLine(magnification * point1X + 2,
									-magnification * point1Y + 2, magnification
											* point2X + 2, -magnification
											* point2Y + 2);
							g.drawString("L(" + i + "," + j + ")" + "="
									+ lineLongth, magnification * showLongthX,
									-magnification * showLongthY);
						}
					}
				}
			}

		}
		
		public void showEdge(){
			isShowEdge = true;
			repaint();
		}
		
		public void hideEdge(){
			isShowEdge = false;
			repaint();
		}
		
		public int getMaxX(int[][] vertexMatrix){
			int max = 0;
			for(int i = 0; i < rows; i++){
				if(vertexMatrix[i][0] > max)
					max = vertexMatrix[i][0];
			}
			return max;
		}
		
		public int getMaxY(int[][] vertexMatrix){
			int max = 0;
			for(int i = 0; i < rows; i++){
				if(vertexMatrix[i][1] > max)
					max = vertexMatrix[i][1];
			}
			return max;
		}
		
		public int getMagnification(int[][] vertexMatrix){
			int mag = 0;
			int maxXCoordinate = getMaxX(vertexMatrix);
			int maxYCoordinate = getMaxY(vertexMatrix);
			int magX = this.getWidth()/maxXCoordinate;
			int magY = this.getHeight()/maxYCoordinate;
			mag = (magX > magY) ? magY : magX;
			return mag;
		}
	}

}
